/*
 * Copyright 2009 BioTeam Inc
 * 
 * Licensed under License GNU LESSER GENERAL PUBLIC LICENSE version 3.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.gnu.org/copyleft/lesser.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package net.bioteam.appweb.submit;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import net.bioteam.appweb.model.Parameter;
import net.bioteam.appweb.model.Program;

/**
 * 
 * @author jason This class simulate a bean class for a program. It accepts the
 *         value from web submission
 */
public class ProgramValueBean implements Serializable
{


	public ProgramValueBean myClone()
	{
		ProgramValueBean bean=new ProgramValueBean();
		bean.program=program;
		bean.valueMap=new HashMap<String, String>();
		bean.valueMap.putAll(valueMap);
		return bean;
	}

	/**
     *
     */
	private static final long serialVersionUID = 1L;
	protected Program program;
	protected Map<String, String> valueMap = new HashMap<String, String>();

	public ProgramValueBean()
	{

	}

	public ProgramValueBean(Program p)
	{
		program = p;
	}

	public Program getProgram()
	{
		return program;
	}

	public Map<String, String> getValueMap()
	{
		return valueMap;
	}

	public void setValueMap(Map<String, String> map)
	{
		valueMap = map;
	}

	public String getDefaultValue(String name)
	{
		if (program.getParameter(name) != null)
		{
			return program.getParameter(name).getDeaultValue();
		} else if (program.getMetaData().get(name) != null)
		{
			return program.getMetaData().get(name).getValue();
		} else
		{
			return null;
		}
	}

	public Parameter getParameter(String name)
	{
		return program.getParameter(name);
	}

	public String getValue(String name)
	{
		String ret = valueMap.get(name);

		if (ret != null)
		{
			return ret;
		}

		if (name.endsWith("_data"))
		{
			return "";
		}

		return getDefaultValue(name);
	}

	public void setValue(String name, String value)
	{
		valueMap.put(name, value);
	}

	public void removeValue(String name)
	{
		valueMap.remove(name);
	}

}
